<template>
  <div>
    <a-card title="PDF文档上传与解析">
      <a-form layout="inline">
        <a-form-item label="用户ID">
          <a-input v-model:value="userId" placeholder="请输入用户ID" style="width: 200px" />
        </a-form-item>
        <a-form-item label="选择PDF文件">
          <input type="file" accept="application/pdf" @change="onFileChange" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" :loading="loading" @click="handleUpload">上传并解析</a-button>
        </a-form-item>
      </a-form>
      <div v-if="resultId" style="margin-top: 16px;">
        <a-alert type="success" :message="`上传成功，文档ID：${resultId}`" show-icon />
      </div>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { pdfApi } from '/@/api/business/pdf/pdf-api';
import { message } from 'ant-design-vue';

// defineOptions({ name: 'PdfUpload' });

const userId = ref('');
const file = ref(null);
const loading = ref(false);
const resultId = ref(null);

function onFileChange(e) {
  file.value = e.target.files[0];
}

async function handleUpload() {
  if (!userId.value) {
    message.warning('请输入用户ID');
    return;
  }
  if (!file.value) {
    message.warning('请选择PDF文件');
    return;
  }
  loading.value = true;
  try {
    const res = await pdfApi.upload(file.value, userId.value);
    resultId.value = res.data;
    message.success('上传并解析成功');
  } catch (err) {
    message.error('上传失败');
  } finally {
    loading.value = false;
  }
}
</script> 